package Presentation;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import Logic.Movie;
import Logic.Person;
import Logic.Rank;
import Logic.User;

public class MainFrame extends JFrame{

	private MainFascade facade;
	private MenuBar menuBar;
	private JPanel headerPanel;
	private JPanel mainPanel;
	private JPanel userDetailsPanel;
	private JPanel movieDetailsPanel;
	private JPanel ranksPanel;
	private JPanel myRankPanel;
	private JPanel loginPanel;
	private JLabel headerL;
	private User logged;
	private Movie movie;
	
	public MainFrame(FacadeUI facade){
		super("FriendSheep!");
		this.facade = facade;
		try{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception ex){}

		setPreferredSize(new Dimension(800,700));
		setLayout(null);
		setResizable(false);
		addWindowListener (new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				shutDown();
	             System.exit(0);
			}
		});		
		
		headerPanel = new JPanel();
		headerPanel.setLayout(null);
		headerPanel.setSize(800,50);
		headerPanel.setLocation(0,0);
		add(headerPanel);
		headerL = new MyLabel("Please login or register as new user!",400,25,200,20);
		headerL.setFont(new Font("Arial",Font.BOLD,20));
		headerL.setHorizontalAlignment(JLabel.CENTER);
		headerPanel.add(headerL);		
		
		
		
		
		mainPanel = new JPanel();
		mainPanel.setLayout(null);
		mainPanel.setSize(800,650);
		mainPanel.setLocation(0,50);
		add(mainPanel);
		
		menuBar = new MenuBar(this);
		setJMenuBar(menuBar);	
		
		
				
		pack();
		setVisible(true);
		
	}
	
	public void showUserPanel(User user,String header, boolean editable){
		mainPanel.removeAll();
		
		userDetailsPanel = new UserDetailsPanel(this,user,header,editable);
		userDetailsPanel.setLocation(25,20);								
		mainPanel.add(userDetailsPanel);
		if (user != null){
			ranksPanel = new RanksPanel(this,user.getRanks(),"User Ranks",1); //1 means by user
			ranksPanel.setLocation(425,20);								
			mainPanel.add(ranksPanel);
		}		
		
		mainPanel.repaint();
	}
	
	public void showLoginPanel(){
		mainPanel.removeAll();
		loginPanel = new LoginPanel(this);
		loginPanel.setLocation(275,20);								
		mainPanel.add(loginPanel);
		mainPanel.repaint();
	}
	
		
	public void login(String username, char[] passwordC){
		
		String password = "";
		for (char c : passwordC)
			password += c;
		if (username == null || username.equals("") || password == null || password.equals("")){
			JOptionPane.showMessageDialog(null, "Username and password mustn't be empty","Login Error" , JOptionPane.ERROR_MESSAGE);
			return;
		}
		logged = facade.login(username, password);		
		if (logged != null){			
			mainPanel.remove(loginPanel);
			headerL.setText("Welcome, " + logged.getFirstName() + " " + logged.getLastName());
			menuBar.openUserOptions();
			if (logged.getUsername().equals("admin")){
				menuBar.addAdminOptions();				
				setJMenuBar(menuBar);
			}
			showUserPanel(logged,"User Details",false);	
			menuBar.repaint();
			mainPanel.repaint();
		}
		else {
			JOptionPane.showMessageDialog(null, "Incorrect Username or Password","Login Error" , JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void logout(){
		mainPanel.removeAll();
		headerL.setText("Please login or register as new user!");
	}
	
	public void showNewMoviePanel(){
		mainPanel.removeAll();
		movieDetailsPanel = new MovieDetailsPanel(this,null,"Add New Movie",true);
		movieDetailsPanel.setLocation(225,20);
		mainPanel.add(movieDetailsPanel);		
		mainPanel.repaint();
		
	}
	
	public void findMovieByName(String name){
		movie = facade.findMovieByName(name);
		 if (movie == null)
			 JOptionPane.showMessageDialog(null, "Movie Not Found...","Message",JOptionPane.PLAIN_MESSAGE);
		 else{ 
			mainPanel.removeAll();
			movieDetailsPanel = new MovieDetailsPanel(this,movie,"Movie Details",true);
			movieDetailsPanel.setLocation(25,20);
			mainPanel.add(movieDetailsPanel);
			if (movie != null){
				showRanksData();
			}		
			mainPanel.repaint();
		 }		
	}
	
	public void showRanksData(){
		Vector<Rank> ranks = movie.getRanks();
		ranksPanel = new RanksPanel(this,ranks,"Movie Ranks",2); //2 means by movie
		ranksPanel.setLocation(425,20);								
		mainPanel.add(ranksPanel);
		Rank myRank = null;
		for (Rank r : ranks){
			if (r.getUser().equals(logged)){
				myRank = r;
				break;
			}
		}
		myRankPanel = new MyRankPanel(this,myRank);
		myRankPanel.setLocation(425,470);								
		mainPanel.add(myRankPanel);
		mainPanel.repaint();
	}
	
	public void addNewMovie(String[] vals){
		facade.addMovie(vals);
	}
	
	public void addNewUser(String[] vals){
		facade.addUser(vals);
	}
	
	public void addRank(int rank){
		facade.rankMovie(logged, movie, rank);
	}
	
	public void refreshMyRanks(){
		mainPanel.remove(ranksPanel);
		mainPanel.remove(myRankPanel);
		showRanksData();
		//mainPanel.repaint();
	}
	
	public void shutDown(){
		facade.shutDown();
		System.exit(0);
	}
	
	
}
